package cn.lbl.face.link;
/*
输入两个链表，找出它们的第一个公共结点。
 */
public class 两个链表的第一个公共结点 {
    private static ListNode temp;
    public static ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        temp= pHead2;
        while (pHead1!=null){
            pHead2=temp;
            while (pHead2!=null){
                if (pHead1 == pHead2) {
                    return pHead1;
                }else {
                    pHead2=pHead2.next;
                    continue;
                }
            }
            pHead1=pHead1.next;
        }
        return null;
    }

    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode commlistNode = new ListNode(3);
        ListNode newlistNode1 = new ListNode(1);
        ListNode newlistNode2 = new ListNode(2);

        listNode1.next=listNode2;
        listNode2.next=commlistNode;

        newlistNode1.next=newlistNode2;
        newlistNode2.next=commlistNode;
        FindFirstCommonNode(listNode1,newlistNode1);
    }
}
